<template>

  <div>

    <!--搜索框部分-->
    <el-card>

      <el-form inline label-width="90px" size="mini" class="demo-form-inline">

        <el-form-item label="歌曲名">
          <el-input placeholder="请输入内容" v-model.trim="searchMusicParam.name" clearable size="mini"></el-input>
        </el-form-item>

        <el-form-item label="歌手">
          <el-input placeholder="请输入歌手" v-model.trim="searchMusicParam.singer" clearable size="mini"></el-input>
        </el-form-item>

        <el-form-item label="音乐类型">
          <el-select v-model="searchMusicParam.typeId" placeholder="请选择音乐类型" clearable>
            <el-option
              v-for="item in musicType"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="评分">
          <el-select v-model="searchMusicParam.grade" placeholder="请选择评价" clearable>
            <el-option label="☆ 浪费时间" :value="1"></el-option>
            <el-option label="☆☆ 勉强下咽" :value="2"></el-option>
            <el-option label="☆☆☆ 一般" :value="3"></el-option>
            <el-option label="☆☆☆☆ 优秀" :value="4"></el-option>
            <el-option label="☆☆☆☆☆ 优质" :value="5"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="专辑名称">
          <el-input placeholder="请输入专辑名称" v-model.trim="searchMusicParam.album" clearable size="mini"></el-input>
        </el-form-item>

        <el-form-item label="作词人">
          <el-input placeholder="请输入作词人" v-model.trim="searchMusicParam.lyrics" clearable size="mini"></el-input>
        </el-form-item>


        <el-form-item>
          <el-button type="primary" size="mini" @click="getMusicListPageBefore">查询</el-button>
          <el-button type="warning" size="mini" @click="resetQueryMusic">重置</el-button>
          <el-button type="warning" size="mini" @click="openSaveMusic">新增</el-button>
        </el-form-item>

      </el-form>

    </el-card>


    <el-card style="margin-top: 10px">

      <el-table
        :data="musicTable.musicData"
        max-height="600px"
        height="600px"
        style="width: 100%">

        <el-table-column
          prop="name"
          label="歌曲名称"
        >
        </el-table-column>

        <el-table-column
          prop="musicTypeName"
          label="音乐类型"
        >
        </el-table-column>

        <el-table-column
          prop="grade"
          label="评分"
        >
          <template slot-scope="scope">
            <el-rate disabled v-model="scope.row.grade"></el-rate>
          </template>
        </el-table-column>

        <el-table-column
          prop="singer"
          label="歌手"
        >
        </el-table-column>

        <el-table-column
          prop="lyrics"
          label="作词"
        >
        </el-table-column>

        <el-table-column
          prop="album"
          label="专辑"
        >
        </el-table-column>

        <el-table-column
          fixed="right"
          label="操作"
          width="180">
          <template slot-scope="scope">
            <el-button @click="toOpenMusicDetailed(scope.row)" type="text" size="small"><i class="el-icon-view"></i>查看</el-button>
            <el-button @click="openUpdMusic(scope.row)" type="text" size="small"><i class="el-icon-edit"></i>编辑</el-button>
            <el-button @click="delMusicByIdTips(scope.row)" type="text" size="small"><i class="el-icon-delete"></i>删除</el-button>
          </template>
        </el-table-column>

      </el-table>

      <!--分页部分-->
      <el-pagination
        style="width:33%; margin-right:10px; margin-top:10px; "
        background
        layout="total, prev, pager, next, jumper"
        @current-change="musicPageCurrentChange"
        :page-size="musicTable.musicSize"
        :current-page="musicTable.musicCurrent"
        :pager-count="5"
        :total="musicTable.musicTotal">
      </el-pagination>

    </el-card>

    <MusicSaveUpd ref="musicSaveUpd" @getMusicListPage="getMusicListPage"></MusicSaveUpd>
    <MusicDetailed ref="musicDetailedRef"></MusicDetailed>

  </div>

</template>

<script>

  import http from '@/utils/http'
  import MusicSaveUpd from './MusicSaveUpd.vue'
  import MusicDetailed from './MusicDetailed.vue'

  export default {
    name: "MusicList",
    components: {
      MusicSaveUpd,
      MusicDetailed,
    },
    data(){
      return {
        // 查询参数
        searchMusicParam: {
          name: null,
          singer: null,
          typeId: null,
          grade: null,
          album: null,
          lyrics: null,
        },
        // 音乐类型
        musicType: [],
        // 表格
        musicTable: {
          // 表格数据
          musicData: [],
          // 每页记录数-字典
          musicSize: 30,
          // 当前页码 - 字典
          musicCurrent: 1,
          // 总记录数 - 字典
          musicTotal: 0,
        },
      }
    },
    methods: {
      /**
       * 跳转打开音乐详情页面
       */
      toOpenMusicDetailed(music){
        this.$refs.musicDetailedRef.openMusicDetailed(music)
      },
      /**
       * 根据ID删除音乐 提示信息
       * @param row 当前行数据
       */
      delMusicByIdTips(row){
        this.$confirm('是否删除该音乐, 删除后无法恢复，是否继续?', '提示', {
          confirmButtonText: '确定删除',
          cancelButtonText: '取消删除',
          type: 'warning'
        }).then(() => {
          // 确定删除
          this.delMusicById(row)

        }).catch(() => {
          // 取消删除
        });
      },
      /**
       * 根据ID删除音乐
       * @param row 当前行数据
       */
      delMusicById(row){
        http.post("/music/delMusicById", {
          id: row.id,
        }).then(response => {
          let res = response.data;
          if (res.status === 'error'){
            // 401是token过期跳转的
            if (res.code !== 401){
              this.$message.error(res.msg);
            }
            return
          }
          this.getMusicListPage()
          this.$message.success("删除成功");

        }).catch(error => {
          this.$message.error(error.message);
        })
      },
      /**
       * 打开音乐添加弹出框
       */
      openSaveMusic(){
        this.$refs.musicSaveUpd.openPageMusic('save', null, this.musicType)
      },
      /**
       * 打开-修改音乐
       */
      openUpdMusic(row){
        this.$refs.musicSaveUpd.openPageMusic('upd', row, this.musicType)
      },
      /**
       * 字典分页被改变的时候
       * @param value 当前页
       */
      musicPageCurrentChange(value){
        this.musicTable.musicCurrent = value
        this.getMusicListPage()
      },
      /**
       * 重置查询音乐参数
       */
      resetQueryMusic() {
        this.searchMusicParam = {
          name: null,
          singer: null,
          typeId: null,
          grade: null,
          album: null,
          lyrics: null,
        }
      },
      /**
       * 查询前重置分页
       */
      getMusicListPageBefore(){
        this.musicTable.musicCurrent = 1
        this.getMusicListPage()
      },
      /**
       * 分页查询音乐信息
       */
      getMusicListPage(){
        this.searchMusicParam.current = this.musicTable.musicCurrent
        this.searchMusicParam.size = this.musicTable.musicSize

        http.post("/music/getMusicListPage", this.searchMusicParam).then(response => {
          let res = response.data;
          if (res.status === 'error'){
            // 401是token过期跳转的
            if (res.code !== 401){
              this.$message.error(res.msg);
            }
            return
          }
          // 分页数据
          let data = res.data;
          this.musicTable.musicTotal = data.total;
          this.musicTable.musicCurrent = data.current;
          this.musicTable.musicSize = data.size;
          // 处理数据
          this.musicTable.musicData = data.records

        }).catch(error => {
          this.$message.error(error.message);
        })
      },
      /**
       * 获取书籍类型
       */
      getMusicType(){
        http.post("/music/getMusicType", {}).then(response => {
          let res = response.data;
          if (res.status === 'error'){
            // 401是token过期跳转的
            if (res.code !== 401){
              this.$message.error(res.msg);
            }
            return
          }

          this.musicType = res.data

        }).catch(error => {
          this.$message.error(error.message);
        })
      },
    },
    mounted(){
      this.getMusicType()
      this.getMusicListPage()
    }
  }
</script>

<style scoped>

</style>
